package com.example.demo.controller;

import org.springframework.web.bind.annotation.CrossOrigin;
import com.example.demo.entity.Admin;

import com.example.demo.service.AdminService;

import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//我又回来了
@CrossOrigin
@RestController
@RequestMapping("admin")
public class AdminController {

    @Autowired
    private AdminService adminService;

    //判断管理员登录用户名和密码正确性
@PostMapping("/right")
    public int login(
            @RequestParam("name") String name,
            @RequestParam("password") String password) {
    int result = 0;
    if (adminService.login(name, password)) {
        result = 1;
    }
    return result;
}

    @GetMapping("/pages")
    public PageInfo<Admin> getPagesAdmin(Integer pageNum,Integer pageSize){
        PageInfo<Admin> pageInfo= adminService.findAdminPage(pageNum,pageSize);
        return pageInfo;
    }

    @GetMapping("/g1")
    public List<Admin>getAllAdmin(){
        return adminService.findAdminAll();
    }

    @GetMapping("/g2/{id}")
    public Admin getAdminBYID(@PathVariable Integer id){
        return adminService.findAdminID(id);
    }


    @PostMapping("/hhl")
    public Integer addAdmin(@RequestBody Admin admin){
        System.out.println(admin);
        return adminService.insertAdmin(admin);
    }


    @PutMapping("/u1")
    public  Integer updateAdmin(@RequestBody  Admin admin){

        System.out.println(admin);
        return adminService.updateAdmin(admin);
    }

    @DeleteMapping("/d1/{id}")
    public  Integer deleteAdmin(@PathVariable("id") Integer admin_id ){
        return  adminService.deleteAdmin(admin_id);
    }

}
